package net.ddyule.www.ddyule;

import android.app.Activity;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import net.ddyule.www.ddyule.util.FileUtil;

import java.io.File;

import cn.woblog.android.downloader.DownloadService;
import cn.woblog.android.downloader.callback.DownloadListener;
import cn.woblog.android.downloader.callback.DownloadManager;
import cn.woblog.android.downloader.domain.DownloadInfo;
import cn.woblog.android.downloader.exception.DownloadException;

public class download_info extends Activity {
    public static final String DEFAULT_URL = "http://wdj-qn-apk.wdjcdn.com/d/c0/3311e7a27b2d3b209bf8d02aca26ac0d.apk";
    private DownloadManager downloadManager;
    private DownloadInfo downloadInfo;
    private TextView mTextViewPercent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_info);
        setFinishOnTouchOutside (false);

        mTextViewPercent = findViewById(R.id.tv_percent);
        downloadManager = DownloadService.getDownloadManager(getApplicationContext());

        downloadInfo = downloadManager.getDownloadById(DEFAULT_URL.hashCode());

    }

    @Override public void onBackPressed()
    { // prevent "back" from leaving this activity
        this.finish();
    }

    @Override
    public void onStart()
    {
        super.onStart();
        createDownload();
    }

    private void createDownload() {
        File d = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "d");
        if (!d.exists()) {
            d.mkdirs();
        }
        String path = d.getAbsolutePath().concat("/").concat("a.apk");
        downloadInfo = new DownloadInfo.Builder().setUrl(DEFAULT_URL)
                .setPath(path)
                .build();
        downloadInfo.setDownloadListener(new DownloadListener() {

            @Override
            public void onStart() {
                mTextViewPercent.setText("开始下载");
            }

            @Override
            public void onWaited() {
                mTextViewPercent.setText("等待下载");
            }

            @Override
            public void onPaused() {
                mTextViewPercent.setText("暂停下载");
            }

            @Override
            public void onDownloading(long progress, long size) {
                mTextViewPercent
                        .setText(FileUtil.formatFileSize(progress) + "/" + FileUtil
                                .formatFileSize(size));
            }

            @Override
            public void onRemoved() {
                mTextViewPercent.setText("删除");
            }

            @Override
            public void onDownloadSuccess() {
                mTextViewPercent.setText("下载完成");
            }

            @Override
            public void onDownloadFailed(DownloadException e) {
                mTextViewPercent.setText("下载失败" + e.getCode());
            }
        });
        downloadManager.download(downloadInfo);
    }
}
